package com.xh.bussiness.exam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.exam.bean.request.UserErrorCategoryRequest;
import com.xh.bussiness.exam.model.UserErrorCategory;
import com.xh.bussiness.exam.dao.UserErrorCategoryMapper;
import com.xh.bussiness.exam.service.IUserErrorCategoryService;
import com.xh.bussiness.system.model.Dictionary;
import com.xh.bussiness.system.service.IDictionaryService;
import com.xh.core.service.impl.BaseServiceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xh.core.bean.XhPage;
import com.xh.core.util.StringUtils;

/**
 * <p>
 * 错题类型表 服务实现类
 * </p>
 *
 * @author JQM
 * @since 2019-11-04
 */
@Service
public class UserErrorCategoryServiceImpl extends BaseServiceImpl<UserErrorCategoryMapper, UserErrorCategory> implements IUserErrorCategoryService {
   
	@Autowired
	private IDictionaryService dictionaryService;
	
	@Override
    public XhPage queryUserErrorCategoryPage(XhPage page, UserErrorCategoryRequest bean) {
        QueryWrapper qw=new QueryWrapper<UserErrorCategory>();
        //拼装条件
        qw=prepareParams(bean, qw);

        return (XhPage) this.dao.selectPage(page,qw);
    }

    @Override
    public List<UserErrorCategory> queryUserErrorCategory(UserErrorCategoryRequest bean) {
      
        QueryWrapper<Dictionary> dicqw=new QueryWrapper<Dictionary>();
        dicqw.in("TYPE_CODE", "QUESTION_SOURCE","ERROR_REASON");
        List<Dictionary> dicList = dictionaryService.queryByCondition(dicqw);
        List<UserErrorCategory> list = new ArrayList<UserErrorCategory>();
        dicList.forEach(dic -> {
        	UserErrorCategory errorCategory = new UserErrorCategory(dic.getCode(),dic.getName(),dic.getTypeCode(),dic.getOrderNo(),dic.getIsSystem());
        	list.add(errorCategory);
        });
        QueryWrapper qw=new QueryWrapper<UserErrorCategory>();
        //拼装条件
        qw=prepareParams(bean, qw);
        list.addAll(this.dao.selectList(qw));
        return list;
    }


    /**
     * 准备条件
     * @param bean
     * @param qw
     */
    private QueryWrapper prepareParams(UserErrorCategoryRequest bean, QueryWrapper qw) {
        if(bean.getIds()!=null&&bean.getIds().length>0){
            qw.in("id",bean.getIds());
        }
        if(!StringUtils.isEmpty(bean.getStudentId())){
            qw.eq("student_id",bean.getStudentId());
        }
        qw.orderByAsc("order_index");
        return qw;
    }
}
